package com.project.web.wechat.service.impl.app;

import com.project.common.core.config.prop.PropAttributes;
import com.project.common.core.config.prop.PropConfig;
import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.id.IDUtil;
import com.project.common.core.utils.redis.RedisClient;
import com.project.common.core.utils.redis.RedisConsts;
import com.project.common.core.utils.weChat.WeChatMiniUtil;
import com.project.common.core.utils.weChat.WeChatMpUtil;
import com.project.feign.client.SystemServiceClient;
import com.project.feign.vo.UserInfoVO;
import com.project.web.wechat.mapper.app.UserInfoMapper;
import com.project.web.wechat.model.app.UserInfo;
import com.project.web.wechat.service.app.WxCallBackService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * 微信关键字回复业务处理
 *
 * @Author: wyy
 * @Date: 2020-08-12 17:51
 */
@Service("wxCallBackServiceImpl")
public class WxCallBackServiceImpl implements WxCallBackService {

    /**
     * 日志工具
     */
    public static final Logger log = LoggerFactory.getLogger(WxCallBackServiceImpl.class);

    @Autowired
    private SystemServiceClient systemServiceClient;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 注入redis工具类
     */
    @Resource(name = "redisClient")
    private RedisClient redisClient;

    @Override
    public String handleMpTxt(String text, Map<String, String> requestMap) {
        // 初始化客服消息参数
        String sentContent = "欢迎来到坚小持英语！";
        String toUserOpenId = requestMap.get("FromUserName");
        Result<UserInfoVO> userInfo = systemServiceClient.getByOpenId(toUserOpenId);
        String nickName = userInfo.getData().getNickname();
        if (StringUtils.isBlank(nickName)) {
            nickName = "学员";
        }
        String wxMiniAppId = PropConfig.getProperty(PropAttributes.THIRDPARTY_WX_MINI_APPID);
        String href = "https://www.jainxc.com";

        switch (text) {
            case "发音":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n    今日作业：戳下方链接进入【发音】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=201903041349154560388&courseType=2" + "\'>>点击学习发音课作业<</a>";
                break;
            case "精读":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n    今日作业：戳下方链接进入【精读】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=201901191922268032065&courseType=2" + "\'>>点击学习精读课作业<</a>";
                break;
            case "精听":
                sentContent = "Hi," + nickName + ":"
                        + "\n    太棒啦！恭喜完成直播课学习！"
                        + "\n    今日作业：戳下方链接进入【听力】课程学习哟~"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=201811262128491990071&courseType=2" + "\'>>点击学习语法课作业<</a>";
                break;
            case "口语":
                sentContent = "Hi," + nickName + ":"
                        + "\n    太棒啦！恭喜完成直播课学习！"
                        + "\n    今日作业：戳下方链接进入【口语】课程学习哟~"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=201901261930040225262&courseType=2" + "\'>>点击学习口语课作业<</a>";
                break;
            case "美语":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n    今日作业：戳下方链接进入【美语】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=202006181822590228248&courseType=2" + "\'>>点击学习美语课作业<</a>";
                break;
            case "法律":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成法律直播课学习！"
                        + "\n    今日作业：戳下方链接进入【法律】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=202005011153126669262&courseType=2" + "\'>>点击学习法律课作业<</a>";
                break;
            case "语法":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n    今日作业：戳下方链接进入【语法】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=202001151501596430470&courseType=2" + "\'>>点击学习语法课作业<</a>";
                break;
            case "原著":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n今日作业：戳下方链接进入【原著精读】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=560348421123211264&courseType=2" + "\'>>点击学习原著精读课作业<</a>";
                break;
            case "人人说英语":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n今日作业：戳下方链接进入【人人说英语】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=629684138890231808&courseType=2" + "\'>>点击学习人人说英语课作业<</a>";
                break;
            case "小词":
                sentContent = "Hi," + nickName + ":"
                        + "\n    恭喜完成直播课学习！"
                        + "\n今日作业：戳下方链接进入【万能口语小词】课程学习，记得提交录音打卡，邀请老师进行1对1纠音点评哟~老师点评后可在本公众号收到提示"
                        + "\n\n<a href=\'" + href
                        + "\' data-miniprogram-appid=\'" + wxMiniAppId
                        + "\' data-miniprogram-path=\'" + "pages/login/login?courseBegin=courseBegin&chapterId=649149519627288576&courseType=2" + "\'>>点击学习万能口语小词作业<</a>";
                break;
        }
        WeChatMpUtil.sendCustomMessage(sentContent, toUserOpenId);

        return "success";
    }

    @Override
    public String handleMpSubscribe(Map<String, String> requestMap) {
        // 根据OPENID 调取微信API获取会员信息
        String openId = requestMap.get("FromUserName");
        JSONObject userJson = WeChatMpUtil.getSubScribeUserInfo(openId);
        log.info("\r\n ******* 关注用户信息 ***********{}", userJson.toString());
        if (null == userJson) {
            log.info("获取公众号用户信息失败");
            return "";
        }

        // 根据OPENID查询微信用户信息
        String nickname = userJson.getString("nickname");
        String unionid = userJson.getString("unionid");
        UserInfo dbUser = userInfoMapper.getByUnionId(unionid);
        // 保存会员信息
        if (null == dbUser) {
            dbUser = new UserInfo();
            dbUser.setUserid(String.valueOf(IDUtil.getId()));
            dbUser.setNickname(nickname);
            dbUser.setPortrait(userJson.getString("headimgurl"));
            dbUser.setSex(userJson.getString("sex"));
            dbUser.setOpenid(openId);
            dbUser.setCreatetime(new Date());
            dbUser.setUnionid(unionid);
            dbUser.setProvince(userJson.getString("province"));
            dbUser.setCity(userJson.getString("city"));
            dbUser.setFollowtime(new Date());
            dbUser.setFollowtime(new Date());
            dbUser.setIsfollow("1");
            dbUser.setInvitecommentnum(5);
            dbUser.setSigninDay(0);
            dbUser.setIsTeacher(false);
            dbUser.setSigninDay(0);
            userInfoMapper.save(dbUser);
        } else {// 更新会员信息
            dbUser.setNickname(nickname);
            dbUser.setPortrait(userJson.getString("headimgurl"));
            if (StringUtils.isBlank(dbUser.getOpenid())) {
                dbUser.setOpenid(openId);
            }
            dbUser.setIsfollow("1");
            userInfoMapper.update(dbUser);
        }
        return "success";
    }

    @Override
    public String handleMiniTxt(String text, Map<String, String> requestMap) {
        // 初始化客服消息参数
        String sentContent = "欢迎来到坚小持英语学院小程序！";
        String toUserOpenId = requestMap.get("FromUserName");
        switch (text) {
            case "event":
                sentContent = "Welcome to JXC English Academy!" +
                        "\n" +
                        "\n回复【0】vip课程定制" +
                        "\n回复【1】英语狂欢节抢购预约"+
                        "\n回复【2】报名尖刀特训直播";
                WeChatMiniUtil.sendCustomMessage(sentContent, toUserOpenId);
                break;
            case "0":
                Object mediaRedis = redisClient.get(RedisConsts.WX_MINI_MEDIA_ONE);
                String mediaId = "";
                if (null == mediaRedis) {
                    mediaId = WeChatMiniUtil.uploadTempMedia("http://oss.jianxc.com/yyxyAppMaterial/wx/prime.png");
                } else {
                    mediaId = String.valueOf(mediaRedis);
                }
                WeChatMiniUtil.sendCustomMessageOfMedia(mediaId, toUserOpenId);
                break;
            case "1":
                Object mediaRedisOfTow = redisClient.get(RedisConsts.WX_MINI_MEDIA_TOW);
                String mediaIdOfTow = "";
                if (null == mediaRedisOfTow) {
                    mediaIdOfTow = WeChatMiniUtil.uploadTempMedia("http://oss.jianxc.com/yyxyAppMaterial/wx/tow.png");
                } else {
                    mediaIdOfTow = String.valueOf(mediaRedisOfTow);
                }
                WeChatMiniUtil.sendCustomMessageOfMedia(mediaIdOfTow, toUserOpenId);
                break;
            case "2":
                sentContent = "恭喜报名成功:"
                        + "\n    英语尖刀特训营马上开营了，戳下方链接立刻进群学习哦！👇👇👇👇"
                        + "\n    【http://h5.jianxc.com/activeCode?channelCode=jxcyy8111&dynamicCodeId=573719336288518144】";
                WeChatMiniUtil.sendCustomMessage(sentContent, toUserOpenId);
                break;
//            case "挑战"://挑战赛
//                Object mediaRedisOfTz = redisClient.get(RedisConsts.WX_MINI_MEDIA_TZ);
//                String mediaIdOfTz = "";
//                if (null == mediaRedisOfTz) {
//                    mediaIdOfTz = WeChatMiniUtil.uploadTempMedia("http://oss.jianxc.com/yyxyAppMaterial/wx/tz.png");
//                } else {
//                    mediaIdOfTz = String.valueOf(mediaRedisOfTz);
//                }
//                WeChatMiniUtil.sendCustomMessageOfMedia(mediaIdOfTz, toUserOpenId);
//                break;
//            case "3"://新春活动
//                // 文本消息
//                sentContent = "恭喜获得新春福利特权:"
//                        + "\n    全场商品五折起 + 买二送一 + 新年红包，扫下方二维码立即参与！👇👇👇👇";
//                WeChatMiniUtil.sendCustomMessage(sentContent, toUserOpenId);
//
//                // 图片消息
//                Object mediaRedisOfXC = redisClient.get(RedisConsts.WX_MINI_MEDIA_XC);
//                String mediaIdOfTz = "";
//                if (null == mediaRedisOfXC) {
//                    mediaIdOfTz = WeChatMiniUtil.uploadTempMedia("http://oss.jianxc.com/yyxyAppMaterial/wx/xc.jpeg");
//                } else {
//                    mediaIdOfTz = String.valueOf(mediaRedisOfXC);
//                }
//                WeChatMiniUtil.sendCustomMessageOfMedia(mediaIdOfTz, toUserOpenId);
//                break;
        }

        return "";
    }
}
